package top.longmarch.flow.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.longmarch.flow.dto.SyncComponentDTO;
import top.longmarch.flow.entity.LiteflowTemplateCmp;
import top.longmarch.flow.mapper.LiteflowTemplateCmpMapper;
import top.longmarch.flow.service.ILiteflowTemplateCmpService;
import top.longmarch.flow.utils.PageUtil;
import top.longmarch.parser.comm.ClassInfo;
import top.longmarch.parser.comm.JavaClassAnalyzer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组件信息 服务实现类
 * </p>
 *
 * @author YuYue
 * @since 2024-04-19
 */
@Service
public class LiteflowTemplateCmpServiceImpl extends ServiceImpl<LiteflowTemplateCmpMapper, LiteflowTemplateCmp> implements ILiteflowTemplateCmpService {
    @Override
    public IPage<LiteflowTemplateCmp> search(Map<String, Object> params) {
        IPage<LiteflowTemplateCmp> page = PageUtil.get(params);
        LambdaQueryWrapper<LiteflowTemplateCmp> wrapper = new LambdaQueryWrapper<>();
        // 模糊查询
        // QueryHelper.addFuzzyQuery(wrapper, params, Constant.FUZZY_VALUE, functionList);
        return this.page(page, wrapper);
    }

    @Transactional
    @Override
    public String syncCmp(SyncComponentDTO dto) {
        StringBuilder sb = new StringBuilder();
        List<ClassInfo> classInfoList = JavaClassAnalyzer.analyzeDirectory(dto.getFilePath(),
                "NodeComponent,NodeBooleanComponent,NodeForComponent");
        if (CollectionUtil.isNotEmpty(classInfoList)) {
            List<LiteflowTemplateCmp> insertLiteflowCmpList = new ArrayList<>();
            List<LiteflowTemplateCmp> updateLiteflowCmpList = new ArrayList<>();
            for (ClassInfo classInfo : classInfoList) {
                LiteflowTemplateCmp liteflowCmp = build(classInfo);
                LiteflowTemplateCmp updateCmp = getLiteflowCmp(liteflowCmp);
                if (updateCmp != null) {
                    liteflowCmp.setId(updateCmp.getId());
                    liteflowCmp.setUpdateTime(new Date());
                    updateLiteflowCmpList.add(liteflowCmp);
                } else {
                    liteflowCmp.setCreateTime(new Date());
                    insertLiteflowCmpList.add(liteflowCmp);
                }
            }
            this.saveBatch(insertLiteflowCmpList);
            sb.append("新增组件数量：").append(insertLiteflowCmpList.size());

            if (dto.getCover()) {
                this.updateBatchById(updateLiteflowCmpList);
                sb.append(" 更新组件数量：").append(updateLiteflowCmpList.size());
            }
        }
        return sb.toString();
    }

    private LiteflowTemplateCmp getLiteflowCmp(LiteflowTemplateCmp cmp) {
        return getOne(new LambdaQueryWrapper<LiteflowTemplateCmp>()
                .eq(LiteflowTemplateCmp::getComponentId, cmp.getComponentId())
                .eq(LiteflowTemplateCmp::getClassName, cmp.getClassName())
                .eq(LiteflowTemplateCmp::getSuperClassQualifiedName, cmp.getSuperClassQualifiedName())
                .eq(LiteflowTemplateCmp::getClassQuotation, cmp.getClassQuotation()));
    }

    private LiteflowTemplateCmp build(ClassInfo classInfo) {
        LiteflowTemplateCmp liteflowTemplate = new LiteflowTemplateCmp();
        liteflowTemplate.setClassName(classInfo.getClassName());
        liteflowTemplate.setClassComment(classInfo.getClassComment());
        liteflowTemplate.setComponentAnnotationName(classInfo.getComponentAnnotationName());
        liteflowTemplate.setComponentName(classInfo.getComponentName());
        liteflowTemplate.setComponentId(classInfo.getComponentId());
        liteflowTemplate.setClassQuotation(classInfo.getClassQuotation());
        liteflowTemplate.setSuperClassQualifiedName(classInfo.getSuperClassQualifiedName());
        liteflowTemplate.setLfClassQualifiedName(classInfo.getLfClassQualifiedName());
        liteflowTemplate.setContent(classInfo.getContent());
        return liteflowTemplate;
    }

}
